package com.bzframework.pro.service;

import com.bzframework.pro.constant.CommonConstants;
import com.bzframework.pro.entity.CampusEntity;
import com.bzframework.pro.entity.CourseEntity;
import com.bzframework.pro.model.school.SchoolHouseKeeperBatchResponse;
import lombok.RequiredArgsConstructor;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;

/**
 * @Author bingzhong
 * @Date 2025/3/21 10:50
 * @Description
 */
@Service
@RequiredArgsConstructor
public class CourseService extends GenericReactiveCrudService<CourseEntity, String> {


    public Mono<SchoolHouseKeeperBatchResponse> fetchCourseDataBatch(String accessToken, int offset) {

        return WebClient
                .create(CommonConstants.SCHOOL_HOUSEKEEPER_PRODUCTION_URL)
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/edu/get_shift_batch")
                        .queryParam("access_token", accessToken)
                        .queryParam("updatetime", "19700101")
                        .queryParam("offset", offset)
                        .queryParam("size", 100)
                        .build())
                .retrieve()
                .bodyToMono(SchoolHouseKeeperBatchResponse.class)
                .timeout(Duration.ofSeconds(15)) // 单独请求超时
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1))); // 指数退避重试;;

    }


}
